<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../jquery-3.3.1.min.js"></script>
    <script>
        $(function(){
            // 浏览器的滚动条上下移动的时候  设置导航条的div固定在页面的顶部/回到原来的位置
            $(window).scroll(function(){
                // console.log($(this).scrollTop());
                if ($(this).scrollTop()>$(".top").height()) {
                    $(".nav").css("position","fixed");
                    // nav会按照原先的布局 所以位置不变  但是要让他处于顶部
                    $(".nav").css("top","0px");
                    // 当吧nav脱标后 content会突然挤上来 所以得设置content的marginTop
                    $(".content").css("marginTop",$(".nav").height());
                }else{
                    $(".nav").css("position","");
                    $(".content").css("marginTop",0);
                }
            });
        });
    </script>
</head>
<body>
    <div class="top">
        <img src="img/2b.png" alt="" height="300px" width="100%">
    </div>
    <div class="nav" style="background-color:orange">导航dsaaaaaaaaaaaaaaaaaaaaaaaaaaaaasadsadsadsadsadsadsadasdsadsadsadsadsadsadsadsadsadsadsadsadasdsadsasadsadsadsadasdsadsasadsadsadsadasdsadsa
        aaaaaaaaaaaaaaaaaaaaa<br><br><br>aaaaaaaaaaaaaaaaaaaaadsadsadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasadsadsadsadasdsadsasadsadsadsadasdsadsaaa</div>
    <div class="content">
        <img src="img/2b.png" alt="">
    </div>
</body>
</html>